<template>
  <div>
    <el-form ref="form" :rules="rules" :model="sysDictData" label-width="100px" size="small">
      <el-form-item label="字典类型" prop="dictType">
        <el-input v-model="sysDictData.dictType" :disabled="true" placeholder="请输入字典类型"></el-input>
      </el-form-item>
      <el-form-item label="数据标签" prop="dictLabel">
        <el-input v-model="sysDictData.dictLabel" placeholder="请输入数据标签" clearable></el-input>
      </el-form-item>
      <el-form-item label="数据键值" prop="dictValue">
        <el-input v-model="sysDictData.dictValue" placeholder="请输入数据键值" clearable></el-input>
      </el-form-item>
      <el-form-item label="显示排序" prop="dictSort">
        <el-input-number v-model="sysDictData.dictSort" style="width: 100%" placeholder="请输入显示排序"></el-input-number>
      </el-form-item>
      <el-form-item label="皮肤属性" prop="dictSkin">
        <el-select v-model="sysDictData.dictSkin" style="width: 100%" placeholder="请输入皮肤属性">
            <el-option
              v-for="item in dictSkinOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="sysDictData.remark" placeholder="请输入备注" clearable></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="sysDictData.status">
          <el-radio v-for="item in statusOptions" :key="item.dictValue" :label="item.dictValue">{{item.dictLabel}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  size="small" @click="add">添加</el-button>
        <el-button type="warning" size="small" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import sysDictDataApi from "@/api/system/sysDictData"
export default {
  name: "sys-dict-data-add.vue",
  props:{
    activeType:{
      type:String,
      default:null
    }
  },
  watch:{
    activeType:{
      immediate:true,
      handler:function (newVal,oldVal){
        this.sysDictData.dictType=newVal;
      }
    }
  },
  data(){
    return{
      //要添加的表单数据
      sysDictData:{
        dictLabel:undefined,
        dictType:undefined,
        dictValue:undefined,
        dictSort:0,
        dictSkin:undefined,
        remark:undefined,
        status:0
      },
      dictSkinOptions: [
        {value: "",label: "默认"},
        {value: "success", label: "成功"},
        {value: "info", label: "信息"},
        {value: "warning", label: "警告"},
        {value: "danger",label: "危险"}
      ],
      //表单验证
      rules: {
        dictLabel: [
          { required: true, message: "数据标签不能为空", trigger: "blur" }
        ],
        dictValue: [
          { required: true, message: "数据键值不能为空", trigger: "blur" }
        ],
        dictSort: [
          { required: true, message: "数据顺序不能为空", trigger: "blur" }
        ]
      },
      //数据状态的字典数据
      statusOptions:[]
    }
  },
  created() {
    this.getDataByType("sys_status_type").then(res=>{
      this.statusOptions=res.data;
    })
  },
  methods:{
    resetForm(){
      this.sysDictData={}
      this.sysDictData.dictType=this.activeType
      this.sysDictData.dictSort=0
      this.sysDictData.status=0
    },
    //保存
    add(){
      //触发表单验证
      this.$refs["form"].validate(valid=>{
        if(valid){
          sysDictDataApi.save(this.sysDictData).then(res=>{
            this.$message.success(res.msg)
            this.$emit("after")
            this.$emit("close")
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
